﻿using B.Auth.APi.Application.Command.Users;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand
{
    public class UserShowsCommandHand : IRequestHandler<UserShowsCommand, ApiResult<List<UserDto>>>
    {
        private readonly IBaseRepository<User> _userdb;
        private readonly IBaseRepository<Province> _province;
        private readonly IBaseRepository<City> _city;
        private readonly IBaseRepository<County> _county;
        private readonly IBaseRepository<RoleModel> _role;
        private readonly IBaseRepository<RoleUser> _roleuser;

        public UserShowsCommandHand(IBaseRepository<User> userdb, IBaseRepository<Province> province, IBaseRepository<City> city, IBaseRepository<County> county, IBaseRepository<RoleModel> role, IBaseRepository<RoleUser> roleuser)
        {
            _userdb = userdb;
            _province = province;
            _city = city;
            _county = county;
            _role = role;
            _roleuser = roleuser;
        }

        public Task<ApiResult<List<UserDto>>> Handle(UserShowsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<UserDto>> result = new ApiResult<List<UserDto>>();

            var UserDto = new List<UserDto>();
            var users = _userdb.GetAll().ToList();
            var provincs=_province.GetAll().ToList();
            var city=_city.GetAll().ToList();
            var county=_county.GetAll().ToList();
            
            foreach (var user in users)
            {

                UserDto.Add(new UserDto
                {

                    Id = user.Id,
                    ProvinceName = _province.GetAll().FirstOrDefault(x => x.Id ==user.FromProvince)?.ProvinceName??"",
                    CityName = _city.GetAll().FirstOrDefault(x => x.Id ==user.FromCity)?.CityName??"",
                    CountyName = _county.GetAll().FirstOrDefault(x => x.Id == user.FromCounty)?.CountyName ?? "",
                    Name = user.Name,
                    Sex = user.Sex,
                    FromProvince = user.FromProvince,
                    FromCity = user.FromCity,
                    FromCounty = user.FromCounty,
                    Addressmessage = user.Addressmessage,
                    CreateUserName = user.CreateUserName,
                    UpdateUserName = user.UodateUserName,
                    
                    CreateTime = user.CreateTime,
                    UpdateTime = user.UpdateTime,
                    RoleName = RoleName(user.Id),
                    departid = user.departid,
                    Del=user.Del,
                   


                });
                    
            
            }
            result.data = UserDto.OrderByDescending(x => x.Id).Where(x=>x.Del==0).ToList();
            return Task.FromResult(result);
         

        }


        public string RoleName(int id)
        {

            List<int> RoleId = _roleuser.GetAll().Where(x => x.UserId == id).Select(x => x.RoleId).ToList();

            var RoleNames = "";
            foreach (var item in RoleId)
            {

                RoleNames += _role.GetAll().FirstOrDefault(x => x.Id == item).RoleName + ',';

            }
            return RoleNames.TrimEnd(',');

        }

    }
}
